/**
 * 
 */
package com.jm.web.builder.form;

import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.form.baseitem.MultiSelectItem;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 * Dec 10, 2011 8:13:19 AM
 */
public class MultiSelectBuilder extends BaseBuilder{
	protected MultiSelectItem multiSelectItem = null;
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		multiSelectItem = new MultiSelectItem();
		multiSelectItem.InitTag(tag);
		multiSelectItem.setFromTag(true);//设置此控件是tag生成的
		multiSelectItem.init();
	}
	
	@Override
	public void RenderEnd() {
		//注册数据
		if(!this.getBaseTag().getPage().autoDatacontainsKey(multiSelectItem.getName()) &&(this.getBaseTag().IsContainsKey("sql") || this.getBaseTag().IsContainsKey("codeList"))){
			try {
				this.getBaseTag().getPage().addMultisData(multiSelectItem.getName());
				
				this.getOut().println("<script>");
				this.getOut().print("$P.multisdata['");
				this.getOut().print(multiSelectItem.getName());
				this.getOut().print("']=");
				if(StringHelper.IsNullOrEmpty(multiSelectItem.getOtherdata())){
					this.getOut().print("[");
					this.getOut().print(multiSelectItem.getData().toString());
					this.getOut().print("];");
				}else{
					this.getOut().print("$P.multisdata['");
					this.getOut().print(multiSelectItem.getOtherdata());
					this.getOut().print("'];");
				}
				this.getOut().println("</script>");
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		//注册选择事件
		StringBuilderEx sb = new StringBuilderEx();
		BaseDataEntity tempConfig = multiSelectItem.getConfig("config");
//		sb.Append("var sys_%1$s_select = function (){",multiSelectItem.getId());
//			sb.Append("var url = '%1$s';",tempConfig.gpsv("url",this.getBaseTag().getPage().getContextPath()+"/include/template/select.jsp"));
//			sb.Append("if(url.indexOf('?') < 0){url+='?';}");
//			//sb.Append("var urlparams = {} ;");
//			//sb.Append("urlparams['single']='%1$s';",tempConfig.gpbv("single",false));
//			sb.Append("url+='&single='+%1$s;",tempConfig.gpbv("single",false));
//			sb.Append("url+='&value='+$P.encode($('#%1$s').val());",multiSelectItem.getId());
//			sb.Append("var params = {} ;");
//			//sb.Append("params['id']='%1$s';",multiSelectItem.getId());
//			//sb.Append("params['name']='%1$s';",multiSelectItem.getName());
//			sb.Append("params['data'] = $P.multisdata['%1$s'];",multiSelectItem.getId());
//			sb.Append("params['type'] = '%1$s';",multiSelectItem.getType());
//			sb.Append("params['value'] = $('#%1$s').val();",multiSelectItem.getId());
//			sb.Append("$.extend(params,{%1$s});",multiSelectItem.GetExtValue("config",""));
//			
//			sb.Append("if('table' == params['type']){");
//				sb.Append("var value = '';");
//				sb.Append("$('[name='+params['name']+']').each(function(){var tempvalue = $P.gv($(this).val());if(tempvalue != ''){value += (params['split']+$(this).val());}});");
//				sb.Append("params['value'] = value;");
//			sb.Append("}");
//			
//			sb.Append("var ret = $P.showmodaldialog1(url,params,'',%1$s,%2$s);",multiSelectItem.GetExtValue("windowWidth","400"),multiSelectItem.GetExtValue("windowHeight","400"));
//			sb.Append("if(ret){");
//				sb.Append("var _me = $('#%1$s');",multiSelectItem.getId());
//				sb.Append("if(ret['action'] == 'clear'){");
//					sb.Append("_me.val('');",multiSelectItem.getId());
//				sb.Append("}else if(ret['action'] == 'set'){");
//					sb.Append("if('table' == params['type']){");
//						sb.Append("ret['id']='%1$s';",multiSelectItem.getName());
//						sb.Append("ret['name']='%1$s';",multiSelectItem.getId());
//						sb.Append("ret['columncnt']=params['columncnt'];");
//						sb.Append("$P.processmultiselect(ret);");
//					sb.Append("}else{");
//						sb.Append("var oldvalue = _me.val();",multiSelectItem.getId());
//						sb.Append("_me.val($P.gv(ret['value']));",multiSelectItem.getId());
//						sb.Append("if(_me.val() != oldvalue)_me.trigger('change',{value:_me.val(),oldvalue:oldvalue});",multiSelectItem.getId());
//					sb.Append("}");
//					
//					sb.Append("var _onpicked=%1$s;",multiSelectItem.GetExtValue("onPicked","null"));
//					sb.Append("if(_onpicked != null ){");
//						sb.Append("if((typeof _picked) == 'string'){");
//							sb.Append("eval(_picked);");
//						sb.Append("}else if((typeof _picked) == 'function'){");
//							sb.Append("_onpicked.call(_me,_RET);");
//						sb.Append("}");
//					sb.Append("}");
//					sb.Append("_me.trigger('picked',ret);",multiSelectItem.getId());
//				sb.Append("};");
//				
//				
//				
//			sb.Append("};");
//		sb.Append("};");
		
//		sb.Append("$('#%1$s_select').click(sys_%1$s_select);",multiSelectItem.getId());
//		this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
		
		//处理MultiSelect 连跳事件 
		if(multiSelectItem.IsContainsKey("jumpConfig")){
			sb.Reset();
			String strConfig = StringHelper.Format("{%1$s}",multiSelectItem.GetExtValue("jumpConfig","").toLowerCase());
			sb.Append("var config_%2$s = %1$s;",strConfig,multiSelectItem.getId());
			sb.Append("var ajax_jump_%1$s = function(args){",multiSelectItem.getId());
			sb.Append("var id = '%1$s';",multiSelectItem.getId());
			sb.Append("var _val = $(this).val();if(args && args['value'])_val=args['value'];");
			sb.Append("$P.processmultisdata(id,null);",multiSelectItem.getId());
			sb.Append("if($P.isnullorempty(_val)){$('#'+id).trigger('picked');return;}");
			
			sb.Append("var p = {};");
			sb.Append("p['action']='ajax_getMultidata';");
			sb.Append("p['params']='parentvalue';");
			sb.Append("p['parentvalue']=_val;");
			sb.Append("$.extend(p,config_%1$s);",multiSelectItem.getId());
			sb.Append("if((typeof p['paramsfun']) == 'function'){p['paramsfun'].call(null,p);delete p['paramsfun'];}");
			
			sb.Append("var doaction = function(jo){");
				sb.Append("if(jo != undefined && jo.ret == 1 && jo.data){");
				sb.Append("$P.processmultisdata(id,jo.data['multisdata']);$('#'+id).trigger('picked');",multiSelectItem.getId());
				sb.Append("};");
			sb.Append("};");
			sb.Append("$P.ajax({data : p,succ :doaction,url:'"+this.getBaseTag().getPage().getContextPath()+"/include/commonajax_backend.jsp'});");
			sb.Append("};");
			
			sb.Append("var xtype = config_%1$s['xtype'];",multiSelectItem.getId());
			sb.Append("var parentid = config_%1$s.parentid;",multiSelectItem.getId());
			sb.Append("var parentel = $('#'+parentid);",multiSelectItem.getId());
			sb.Append("if(parentel.gv('xtype') != '') xtype=parentel.gv('xtype');");
			
			sb.Append("if('org' == xtype){");
				sb.Append("parentel.bind('picked',ajax_jump_%1$s);",multiSelectItem.getId());
			sb.Append("}else if('combobox' == xtype || 'combobox_text' == xtype){");
				sb.Append("parentel.bind('picked',ajax_jump_%1$s);",multiSelectItem.getId());
				sb.Append("parentel.bind('change',function(){if($(this).val().trim() == ''){$('#'+config_%1$s.parentid).trigger('picked',this);}});",multiSelectItem.getId());
			sb.Append("}else if('multiselect' == xtype){");
				sb.Append("parentel.bind('picked',ajax_jump_%1$s);",multiSelectItem.getId());
			sb.Append("}else if('text' == xtype){");
				sb.Append("parentel.blur(ajax_jump_%1$s);",multiSelectItem.getId());
			sb.Append("}else{");
				sb.Append("parentel.change(ajax_jump_%1$s);",multiSelectItem.getId());
			sb.Append("}");
			
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
			multiSelectItem.RemoveExtValue("jumpConfig");
		}
		
		multiSelectItem.onRender2(this.getOut());
	}
}
